package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.csmc.domain.TCMchntAcctDetail;
import cc.rengu.redp.bizimpl.enums.BizDataAuditStatusEnum;
import cc.rengu.redp.bizimpl.enums.BizDataStatusEnum;
import cc.rengu.redp.bizimpl.enums.BizOprEnum;
import cc.rengu.redp.bizimpl.enums.PageOptFlagEnum;
import cc.rengu.redp.bizimpl.upmp.domain.BTxnLimitCfg;
import cc.rengu.redp.bizimpl.upmp.service.BTxnLimitCfgService;
import cc.rengu.redp.bizimpl.igas.service.IPTermKeyService;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.*;
import cc.rengu.redp.bizimpl.upmp.dao.BTermInfoMapper;
import cc.rengu.redp.bizimpl.upmp.service.*;
import cc.rengu.redp.bizimpl.upmp.vo.BTermInfoVO;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.SortUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author lizk
 */
@Service
@DS("upmp")
public class BTermInfoServiceImpl extends ServiceImpl<BTermInfoMapper, BTermInfo> implements IBTermInfoService {

    @Autowired
    private IPTermKeyService ipTermKeyService;
    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Autowired
    private IUpmpOrganizationService ibOrgInfoService;
    @Autowired
    private IUpmpSysSeqService sysSeqService;
    @Autowired
    private IUpmpBMchntBaseInfoService iUpmpBMchntBaseInfoService;
    @Autowired
    private RegionInfoService regionInfoService;
    @Autowired
    private IUpmpPrivilegeCheckService authPrivilegeCheckService;
    @Autowired
    private IBizTmpService bizTmpService;
    @Autowired
    private IBizHisService bizHisService;
    @Autowired
    private BTxnLimitCfgService bTxnLimitCfgService;
    @Autowired
    private IBDeviceInfoService deviceInfoService;

    private long oprMenuId = 20000033;
    private String oprMenuName = "终端管理:间联终端信息管理";
    private long chkMenuId = 20000033;
    private String chkMenuName = "终端管理:间联终端信息管理";
    private String webNextUrl = "/info";


    @Override
    public IPage<BTermInfo> findByPage(QueryRequest queryRequest, BTermInfoVO termInfo) {
        try {
            LambdaQueryWrapper<BTermInfo> queryWrapper = new LambdaQueryWrapper<>();
            if (!StringUtils.isEmpty(termInfo.getInstId())) {
                queryWrapper.eq(BTermInfo::getInstId, termInfo.getInstId());
            }
            if (!StringUtils.isEmpty(termInfo.getMchntNo())) {
                queryWrapper.and(i -> i.like(BTermInfo::getMchntNo, termInfo.getMchntNo())
                        .or().like(BTermInfo::getTermNo, termInfo.getTermNo())
                        .or().like(BTermInfo::getTermPrintMerName, termInfo.getTermPrintMerName()));
            }

            Page<BTermInfo> page = new Page<>();

            SortUtil.handlePageSort(queryRequest, page, true);

            //设置模型数量
            Page<BTermInfo> infoPage = this.page(page, queryWrapper);
            for (BTermInfo bTermInfo : infoPage.getRecords()) {
                int result = ipTermKeyService.count(bTermInfo.getInstId(), bTermInfo.getMchntNo(), bTermInfo.getTermNo(), "00");
                if (result > 0) {
                    bTermInfo.setKeyDownFlag("已下载");
                }
                UpmpOrganization bOrgInfo = ibOrgInfoService.getOrgByCode(bTermInfo.getPosInstCd(), bTermInfo.getInstId());
                if (!ObjectUtils.isEmpty(bOrgInfo)) {
                    bTermInfo.setPosInst(bOrgInfo.getOrgCode() + "-" + bOrgInfo.getOrgName());
                }
            }

            return infoPage;
        } catch (Exception e) {
            log.error("获取间联终端信息失败", e);
            return null;
        }
    }

    @Override
    public void createInfo(BTermInfoVO info) throws RedpException {
        saveData(info);

    }

    @Override
    public void updateInfo(BTermInfoVO info) throws RedpException {
        saveData(info);
    }

    @Override
    public void resetKey(BTermInfoVO info) {
        String instId = info.getInstId();
        String mchntNo = info.getMchntNo();
        String termNo = info.getTermNo();
        String keyType = info.getKeyType();
        ipTermKeyService.resetKey(instId, mchntNo, termNo, keyType);
    }

    @Override
    public void infoStatusChange(BTermInfoVO info) throws RedpException {

        saveData(info);

    }

    @Override
    public void batchInsert(List<BTermInfoVO> list) throws RedpException {

        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
//                Organization org = organizationService.getOrgByCode(user.getOrgCode(), user.getInstId());
        SimpleDateFormat fmtdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String lastUpddt = fmtdt.format(new Date());
        //验证数据

        //保存数据
        UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(list.get(0).getInstId(), "info:add");
        for (BTermInfoVO bTermInfoVO : list) {
            BTxnLimitCfg bTxnLimitCfg = null;
            BTermInfo termInfo = new BTermInfo();
            //获取终端编号
            String termNo = bTermInfoVO.getInstId() + String.format("%05d", Integer.parseInt(sysSeqService
                    .updateParamValue(BusinessContainer.COMM_PARAM_CODE, BusinessContainer.MachiCd_PREFIX, null)));

            bTermInfoVO.setTermNo(termNo);
            bTermInfoVO.setCupsReportStatus("00");
            if (org.apache.commons.lang3.StringUtils.isNotBlank(bTermInfoVO.getSingleLimitMaxAmt()) && org.apache.commons.lang3.StringUtils.isNotBlank(bTermInfoVO.getDayLimitAmt()) && org.apache.commons.lang3.StringUtils.isNotBlank(bTermInfoVO.getRemainLimitAmt())) {
                /*保存限额信息*/
                bTxnLimitCfg = new BTxnLimitCfg();
                bTxnLimitCfg.setInstId(bTermInfoVO.getInstId());
                bTxnLimitCfg.setLimitType("16");
                bTxnLimitCfg.setLimitInst(bTermInfoVO.getMchntNo() + "|" + bTermInfoVO.getTermNo());
                bTxnLimitCfg.setLimitAcctType("*");
                bTxnLimitCfg.setSingleLimitMaxAmt(bTermInfoVO.getSingleLimitMaxAmt());
                bTxnLimitCfg.setDayLimitAmt(bTermInfoVO.getDayLimitAmt());
                bTxnLimitCfg.setMonthLimitAmt(String.valueOf(Integer.parseInt(bTermInfoVO.getDayLimitAmt()) * 31));
                bTxnLimitCfg.setStatus("Y");
                bTxnLimitCfg.setOprId(user.getId());
                bTxnLimitCfg.setCreateTime(lastUpddt);
                bTxnLimitCfg.setId(null);
                bTermInfoVO.setPasswordOffsetValue(generatePswdOstVlCode(bTermInfoVO.getInstId()));
            }
            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                bTermInfoVO.setTermStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                bTermInfoVO.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());

                BeanUtils.copyProperties(bTermInfoVO, termInfo);

                this.save(termInfo);
//                insertTermInfo(termInfo);

                bizTmpService.saveBizTmp(termInfo, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                        String.valueOf(chkMenuId), chkMenuName,
                        termInfo.getTermStatus(), termInfo.getAuditStatus(), termInfo.getId(), "", null, null, null);
                if (null != bTxnLimitCfg) {
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                    bTxnLimitCfgService.saveOrUpdate(bTxnLimitCfg);
                }
            } else {
                BeanUtils.copyProperties(bTermInfoVO, termInfo);
                termInfo.setTermStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                this.save(termInfo);
//                insertTermInfo(termInfo);

                bizHisService.saveBizHisNew(termInfo, termInfo, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                        String.valueOf(oprMenuId), oprMenuName,
                        termInfo.getId(), null, null, null);
                if (null != bTxnLimitCfg) {
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                    bTxnLimitCfgService.saveOrUpdate(bTxnLimitCfg);
                    bizHisService.saveBizHisNew(bTxnLimitCfg, bTxnLimitCfg, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                            String.valueOf(oprMenuId), oprMenuName,
                            bTxnLimitCfg.getId(), null, null, null);
                }
            }
            /*将终端库存sn更新为已使用状态*/
            deviceInfoService.updateTermSn(termInfo.getInstId(), termInfo.getSn(), termInfo.getMchntNo(), termInfo.getTermNo());
        }

    }

    public void saveData(BTermInfoVO entity) throws RedpException {

        String flag = entity.getOptFlag();
        String repayPosFlag = entity.getRepayPosFlag();  //是否开通终端代付
        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
//        Organization org = organizationsService.getOrgByCode(user.getOrgCode(), user.getInstId());
        //停用时发送短信状态 0:无需发送短信、1:发送成功、2:发送失败
        int isSendSmsSuccess = 0;

        BTermInfo oldEntity = null;
        if (!PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(flag)) {
            oldEntity = this.baseMapper.selectById(entity.getId());
        }

        if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(flag)) {
            UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(entity.getInstId(), "info:add");

            int length = user.getInstId().length();
            String NO = user.getInstId().substring(length - 3, length) + String.format("%05d", Integer.parseInt(sysSeqService
                    .updateParamValue(BusinessContainer.COMM_PARAM_CODE, BusinessContainer.MachiCd_PREFIX, null)));
            entity.setTermNo(NO);

            entity.setId(String.valueOf(System.currentTimeMillis()));
            entity.setOprId(user.getId());
            entity.setCreateTime(lastUpddt);
            entity.setLastOprId(user.getId());
            entity.setLastUpdateTime(lastUpddt);

            /*判断是否开通终端pos代付*/
            if (BusinessContainer.YES.equals(repayPosFlag)) {
                /*保存限额信息*/
                BTxnLimitCfg bTxnLimitCfg = new BTxnLimitCfg();
                bTxnLimitCfg.setInstId(entity.getInstId());
                bTxnLimitCfg.setLimitType("16");
                bTxnLimitCfg.setLimitInst(entity.getMchntNo() + "|" + entity.getTermNo());
                bTxnLimitCfg.setLimitAcctType("*");
                bTxnLimitCfg.setSingleLimitMaxAmt(entity.getSingleLimitMaxAmt());
                bTxnLimitCfg.setDayLimitAmt(entity.getDayLimitAmt());
                bTxnLimitCfg.setMonthLimitAmt(String.valueOf(Integer.parseInt(entity.getDayLimitAmt()) * 31));
                bTxnLimitCfg.setStatus("Y");
                bTxnLimitCfg.setOprId(user.getId());
                bTxnLimitCfg.setCreateTime(lastUpddt);
                bTxnLimitCfg.setId(null);
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                    bTxnLimitCfgService.save(bTxnLimitCfg);
                } else {
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                    bTxnLimitCfgService.save(bTxnLimitCfg);
                    bizHisService.saveBizHisNew(bTxnLimitCfg, bTxnLimitCfg, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                            String.valueOf(oprMenuId), oprMenuName,
                            bTxnLimitCfg.getId(), null, null, null);
                }

                entity.setPasswordOffsetValue(generatePswdOstVlCode(entity.getInstId()));
            } else {
                entity.setRemainLimitAmt("");
            }
            BTermInfo bTermInfo = new BTermInfo();
            BeanUtils.copyProperties(entity, bTermInfo);
            bTermInfo.setCupsReportStatus("00");

            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                bTermInfo.setTermStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                bTermInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                this.save(bTermInfo);

                bizTmpService.saveBizTmp(bTermInfo, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                        String.valueOf(chkMenuId), chkMenuName,
                        bTermInfo.getTermStatus(), bTermInfo.getAuditStatus(), bTermInfo.getId(), "", null, null, null);
            } else {
                bTermInfo.setTermStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                bTermInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                this.saveOrUpdate(bTermInfo);

                bizHisService.saveBizHisNew(bTermInfo, bTermInfo, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                        String.valueOf(oprMenuId), oprMenuName,
                        bTermInfo.getId(), null, null, null);
            }
            /*将终端库存sn更新为已使用状态*/
            deviceInfoService.updateTermSn(entity.getInstId(), entity.getSn(), entity.getMchntNo(), entity.getTermNo());
        } else if (PageOptFlagEnum.OPT_FLAG_UPDATE.getType().equals(flag)) {
            String id = entity.getId();
            if (Common.isBlank(id)) {
                log.error("请求参数(id)异常!");
                throw new RedpException("请求参数(id)异常!");
            }

            UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(entity.getInstId(), "info:edit");
            BTermInfo termInfo = this.baseMapper.selectById(id);
            termInfo.setLastOprId(user.getId());
            termInfo.setLastUpdateTime(lastUpddt);
            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                /*判断是否开通终端pos代付*/
                if (BusinessContainer.YES.equals(repayPosFlag)) {
                    List<BTxnLimitCfg> btLimitCfList = bTxnLimitCfgService.findByPDstChannelInfo(termInfo.getInstId(), "16", termInfo.getMchntNo() + "|" + termInfo.getTermNo());
                    if (Common.isNotBlank(btLimitCfList) && btLimitCfList.size() > 0) {
                        BTxnLimitCfg bTxnLimitCfg = btLimitCfList.get(0);
                        bTxnLimitCfg.setLastOprId(user.getId());
                        bTxnLimitCfg.setLastUpdateTime(lastUpddt);
                        bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                        bTxnLimitCfgService.saveOrUpdate(bTxnLimitCfg);
                    } else {
                        /*新增限额信息*/
                        BTxnLimitCfg bTxnLimitCfg = new BTxnLimitCfg();
                        bTxnLimitCfg.setInstId(entity.getInstId());
                        bTxnLimitCfg.setLimitType("16");
                        bTxnLimitCfg.setLimitInst(entity.getMchntNo() + "|" + entity.getTermNo());
                        bTxnLimitCfg.setLimitAcctType("*");
                        bTxnLimitCfg.setSingleLimitMaxAmt(entity.getSingleLimitMaxAmt());
                        bTxnLimitCfg.setDayLimitAmt(entity.getDayLimitAmt());
                        bTxnLimitCfg.setMonthLimitAmt(String.valueOf(Integer.parseInt(entity.getDayLimitAmt()) * 31));
                        bTxnLimitCfg.setStatus("Y");
                        bTxnLimitCfg.setOprId(user.getId());
                        bTxnLimitCfg.setCreateTime(lastUpddt);
                        bTxnLimitCfg.setId(null);
                        bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                        bTxnLimitCfgService.saveOrUpdate(bTxnLimitCfg);
                    }
                    entity.setPasswordOffsetValue(generatePswdOstVlCode(entity.getInstId()));
                } else {
                    entity.setRemainLimitAmt("");
                }

                termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                BeanUtils.copyProperties(entity, termInfo);
                this.saveOrUpdate(termInfo);

                //检查临时表是否有数据，如果没用则新增
                bizTmpService.saveBizTmp(termInfo, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                        String.valueOf(chkMenuId), chkMenuName,
                        termInfo.getTermStatus(), termInfo.getAuditStatus(), termInfo.getId(), "", null, null, null);
            } else {
                /*判断是否开通终端pos代付*/
                if (BusinessContainer.YES.equals(repayPosFlag)) {
                    List<BTxnLimitCfg> btLimitCfList = bTxnLimitCfgService.findByPDstChannelInfo(termInfo.getInstId(), "16", termInfo.getMchntNo() + "|" + termInfo.getTermNo());
                    if (Common.isNotBlank(btLimitCfList) && btLimitCfList.size() > 0) {
                        BTxnLimitCfg oldBTxnLimitCfg = btLimitCfList.get(0);
                        BTxnLimitCfg bTxnLimitCfg = new BTxnLimitCfg();
                        BeanUtils.copyProperties(oldBTxnLimitCfg, bTxnLimitCfg);
                        bTxnLimitCfg.setSingleLimitMaxAmt(entity.getSingleLimitMaxAmt());
                        bTxnLimitCfg.setDayLimitAmt(entity.getDayLimitAmt());
                        bTxnLimitCfg.setMonthLimitAmt(String.valueOf(Integer.parseInt(entity.getDayLimitAmt()) * 31));
                        bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                        bTxnLimitCfgService.saveOrUpdate(bTxnLimitCfg);
                        bizHisService.saveBizHisNew(bTxnLimitCfg, bTxnLimitCfg, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                                String.valueOf(oprMenuId), oprMenuName,
                                bTxnLimitCfg.getId(), null, null, null);
                    } else {
                        /*新增限额信息*/
                        BTxnLimitCfg bTxnLimitCfg = new BTxnLimitCfg();
                        bTxnLimitCfg.setInstId(entity.getInstId());
                        bTxnLimitCfg.setLimitType("16");
                        bTxnLimitCfg.setLimitInst(entity.getMchntNo() + "|" + entity.getTermNo());
                        bTxnLimitCfg.setLimitAcctType("*");
                        bTxnLimitCfg.setSingleLimitMaxAmt(entity.getSingleLimitMaxAmt());
                        bTxnLimitCfg.setDayLimitAmt(entity.getDayLimitAmt());
                        bTxnLimitCfg.setMonthLimitAmt(String.valueOf(Integer.parseInt(entity.getDayLimitAmt()) * 31));
                        bTxnLimitCfg.setStatus("Y");
                        bTxnLimitCfg.setOprId(user.getId());
                        bTxnLimitCfg.setCreateTime(lastUpddt);
                        bTxnLimitCfg.setId(null);
                        bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                        bTxnLimitCfgService.saveOrUpdate(bTxnLimitCfg);
                    }
                    entity.setPasswordOffsetValue(generatePswdOstVlCode(entity.getInstId()));
                } else {
                    entity.setRemainLimitAmt("");
                }

                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());

                BeanUtils.copyProperties(entity, termInfo);
                this.saveOrUpdate(termInfo);

                //历史表新增
                bizHisService.saveBizHisNew(termInfo, termInfo, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                        String.valueOf(oprMenuId), oprMenuName,
                        termInfo.getId(), null, null, null);
            }
        } else if (PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(flag)
                || PageOptFlagEnum.OPT_FLAG_USE.getType().equals(flag)
                || PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(flag)) {
            String id = entity.getId();
            if (Common.isBlank(id)) {
                log.error("请求参数(id)异常!");
                throw new RedpException("请求参数(id)异常!");
            }
            BTermInfo termInfo = this.baseMapper.selectById(id);

            if (Common.isBlank(termInfo)) {
                log.error("根据id没有找到指定数据！");
                throw new RedpException("根据id没有找到指定数据!");
            }

            /*判断是否开通终端pos代付*/
            BTxnLimitCfg bTxnLimitCfg = new BTxnLimitCfg();
            BTxnLimitCfg oldBTxnLimitCfg = new BTxnLimitCfg();
            if (BusinessContainer.YES.equals(repayPosFlag)) {
                List<BTxnLimitCfg> btLimitCfList = bTxnLimitCfgService.findByPDstChannelInfo(termInfo.getInstId(), "16", termInfo.getMchntNo() + "|" + termInfo.getTermNo());
                if (Common.isNotBlank(btLimitCfList) && btLimitCfList.size() > 0) {
                    oldBTxnLimitCfg = btLimitCfList.get(0);
                    BeanUtils.copyProperties(oldBTxnLimitCfg, bTxnLimitCfg);
                }
            }

            UpmpPrivilegeCheck privilegeCheck = new UpmpPrivilegeCheck();
            String bizOpr = "";
            if (PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(flag)) {
                bizOpr = BizOprEnum.OPR_STOP.getType();
                privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(entity.getInstId(), "info:stop");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType());
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType());
                } else {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                    termInfo.setTermStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                    //终端停用发送短信   TODO 待完善
//                    isSendSmsSuccess = sendSMS(termInfo, result);
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                    bTxnLimitCfg.setStatus("N");
                }
            } else if (PageOptFlagEnum.OPT_FLAG_USE.getType().equals(flag)) {

                bizOpr = BizOprEnum.OPR_USE.getType();
                privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(entity.getInstId(), "info:use");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType());
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType());
                } else {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                    termInfo.setTermStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                    bTxnLimitCfg.setStatus("Y");
                }
            } else if (PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(flag)) {

                bizOpr = BizOprEnum.OPR_CANCEL.getType();
                privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(entity.getInstId(), "info:cancel");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType());
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType());
                } else {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                    termInfo.setTermStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                    bTxnLimitCfg.setStatus("N");
                }
            }
            termInfo.setLastOprId(user.getId());
            //termInfo.setTermStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
            termInfo.setLastUpdateTime(lastUpddt);
            this.saveOrUpdate(termInfo);
            /*判断是否开通终端pos代付*/
            if (BusinessContainer.YES.equals(repayPosFlag) && Common.isNotBlank(oldBTxnLimitCfg)) {
                bTxnLimitCfg.setLastOprId(user.getId());
                bTxnLimitCfg.setLastUpdateTime(lastUpddt);
                bTxnLimitCfgService.saveOrUpdate(bTxnLimitCfg);
            }
            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                //检查临时表是否有数据，如果没用则新增
                bizTmpService.saveBizTmp(termInfo, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                        String.valueOf(chkMenuId), chkMenuName,
                        termInfo.getTermStatus(), termInfo.getAuditStatus(), termInfo.getId(), "", null, null, null);

            } else {
                //历史表新增
                bizHisService.saveBizHisNew(termInfo, termInfo, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                        String.valueOf(oprMenuId), oprMenuName,
                        termInfo.getId(), null, null, null);
                /*判断是否开通终端pos代付*/
                if (BusinessContainer.YES.equals(repayPosFlag) && Common.isNotBlank(oldBTxnLimitCfg)) {
                    //历史表新增
                    bizHisService.saveBizHisNew(termInfo, termInfo, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                            String.valueOf(oprMenuId), oprMenuName,
                            termInfo.getId(), null, null, null);
                }
            }
        } else if (PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(flag)) {

            String id = entity.getId();
            if (Common.isBlank(id)) {
                log.error("请求参数(id)异常!");
                throw new RedpException("请求参数(id)异常!");
            }


            //获取临时表数据并转换成实体对象
            BizTmp tmp = bizTmpService.getBizTmpDetail(id);
            if (Common.isBlank(tmp)) {
                log.error("根据id没有找到指定临时表数据！");
                throw new RedpException("根据id没有找到指定临时表数据!");
            }
            String bizdata = "";
            if (null != tmp.getBizData1()) {
                bizdata += tmp.getBizData1();
            }
            if (null != tmp.getBizData2()) {
                bizdata += tmp.getBizData2();
            }
            if (null != tmp.getBizData3()) {
                bizdata += tmp.getBizData3();
            }
            BTermInfo termInfoNew = JSON.parseObject(bizdata, BTermInfo.class);

            BTermInfo termInfo = this.baseMapper.selectById(id);

            if (Common.isBlank(termInfo)) {
                log.error("根据id没有找到指定数据！");
                throw new RedpException("根据id没有找到指定数据!");
            }

            String opt = entity.getOpt();
            if (Common.isBlank(opt)) {
                log.error("请求参数(opt)异常!");
                throw new RedpException("请求参数(opt)异常!");
            }

            String bizOpr = "";
            BeanUtils.copyProperties(termInfoNew, termInfo);

            /*判断是否开通终端pos代付*/
            BTxnLimitCfg bTxnLimitCfg = new BTxnLimitCfg();
            BTxnLimitCfg oldBTxnLimitCfg = new BTxnLimitCfg();
            if (BusinessContainer.YES.equals(repayPosFlag)) {
                List<BTxnLimitCfg> btLimitCfList = bTxnLimitCfgService.findByPDstChannelInfo(termInfo.getInstId(), "16", termInfo.getMchntNo() + "|" + termInfo.getTermNo());
                if (Common.isNotBlank(btLimitCfList) && btLimitCfList.size() > 0) {
                    oldBTxnLimitCfg = btLimitCfList.get(0);
                    BeanUtils.copyProperties(oldBTxnLimitCfg, bTxnLimitCfg);
                }
            }

            String approvalRefuseReason = entity.getApprovalRefuseReason();
            if (BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType().equals(termInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                    termInfo.setTermStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                } else {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_REJECT.getType());
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_ADD.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType().equals(termInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                    termInfo.setTermStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                    bTxnLimitCfg.setStatus("N");
                } else {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_REJECT.getType());
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_CANCEL.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType().equals(termInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                    termInfo.setTermStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                    //终端停用发送短信   TODO
//                    isSendSmsSuccess = sendSMS(termInfo, result);
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                    bTxnLimitCfg.setStatus("N");
                } else {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_REJECT.getType());
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_STOP.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType().equals(termInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                    termInfo.setTermStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                    bTxnLimitCfg.setStatus("Y");
                } else {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_REJECT.getType());
                    termInfo.setTermStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_USE.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType().equals(termInfo.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                    termInfo.setTermStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    bTxnLimitCfg.setSingleLimitMaxAmt(termInfo.getSingleLimitMaxAmt());
                    bTxnLimitCfg.setDayLimitAmt(termInfo.getDayLimitAmt());
                    bTxnLimitCfg.setMonthLimitAmt(String.valueOf(Integer.parseInt(termInfo.getDayLimitAmt()) * 31));
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                } else {
                    termInfo.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_REJECT.getType());
                    bTxnLimitCfg.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_UPDATE.getType();
            }
            termInfo.setAuditRefuseReason(approvalRefuseReason);
            this.saveOrUpdate(termInfo);
            /*判断是否开通终端pos代付*/
            if (BusinessContainer.YES.equals(repayPosFlag) && Common.isNotBlank(oldBTxnLimitCfg)) {
                bTxnLimitCfgService.saveOrUpdate(bTxnLimitCfg);
            }
            if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                //审批通过将正式表更新到历史表
                bizHisService.saveBizHisNew(termInfo, termInfo, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                        String.valueOf(oprMenuId), oprMenuName,
                        termInfo.getId(), null, null, null);
                /*判断是否开通终端pos代付*/
                if (BusinessContainer.YES.equals(repayPosFlag) && Common.isNotBlank(oldBTxnLimitCfg)) {
                    bizHisService.saveBizHisNew(bTxnLimitCfg, oldBTxnLimitCfg, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                            String.valueOf(oprMenuId), oprMenuName,
                            oldBTxnLimitCfg.getId(), null, null, null);
                }
            }
            // 修改临时表的审核状态等字段
            bizTmpService.saveBizTmp(termInfo, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt,
                    String.valueOf(oprMenuId), oprMenuName,
                    termInfo.getTermStatus(), termInfo.getAuditStatus(), termInfo.getId(), "", null, null, null);
        }
    }


    /**
     * 筛选出符合要求的excel导入数据
     *
     * @param bTermInfoVO
     * @return
     */
    @Override
    public BTermInfoVO checkAndDeal(BTermInfoVO bTermInfoVO) {
        //TODO 导入和模板下载存在问题
//        User user = AuthContextHolder.findAuthUser();
//        Organization org = organizationService.getOrgByCode(user.getOrgCode(), user.getInstId());
        SimpleDateFormat fmtdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String lastUpddt = fmtdt.format(new Date());

//        TermInfo termInfo = new TermInfo();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(bTermInfoVO.getMchntNo())) {
            UpmpBMchntBaseInfo mchnt = iUpmpBMchntBaseInfoService.findByMchntNo(bTermInfoVO.getMchntNo());
            if (null != mchnt) {
                bTermInfoVO.setMchntNo(mchnt.getMchntNo());
                bTermInfoVO.setTermPrintMerName(mchnt.getMchntName());
                bTermInfoVO.setPosInstCd(mchnt.getSignInstId());
                //判断商户是否开通终端代付pos
//                Boolean bl = selectMchntRepayPos(mchnt.getMchntNo());
//                String singleLimitMaxAmt = getCellValue(row, 19);
//                String dayLimitAmt = getCellValue(row, 20);
//                String remainLimitAmt = getCellValue(row, 21);
//                if (bl) {
                //开通终端代付pos
//                    if (org.apache.commons.lang3.StringUtils.isBlank(singleLimitMaxAmt) || org.apache.commons.lang3.StringUtils.isBlank(dayLimitAmt) || org.apache.commons.lang3.StringUtils.isBlank(remainLimitAmt)) {
//                        uploadErrMsg += "商户已开通终端代付pos，请填写终端代付限额信息及可用额度；";
//                    }
//                    termInfo.setSingleLimitMaxAmt(singleLimitMaxAmt);
//                    termInfo.setDayLimitAmt(dayLimitAmt);
//                    termInfo.setRemainLimitAmt(remainLimitAmt);
//                } else {
                //未开通终端代付pos
//                    if (org.apache.commons.lang3.StringUtils.isNotBlank(singleLimitMaxAmt) || org.apache.commons.lang3.StringUtils.isNotBlank(dayLimitAmt) || org.apache.commons.lang3.StringUtils.isNotBlank(remainLimitAmt)) {
//                        uploadErrMsg += "商户未开通终端代付pos，不必填写终端代付限额信息及可用额度；";
//                    }
//                }
            } else {
                bTermInfoVO.setCheckFlag("商户编码错误，商户不存在；");
                bTermInfoVO.setColName("商户编码");
                bTermInfoVO.setColumns(0);
                return bTermInfoVO;
            }
        } else {
            bTermInfoVO.setCheckFlag("商户编码为必填项；");
            bTermInfoVO.setColName("商户编码");
            bTermInfoVO.setColumns(0);
            return bTermInfoVO;
        }
        String termType = bTermInfoVO.getTermType();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(termType)) {
            termType = termType.substring(0, termType.indexOf("-"));
            bTermInfoVO.setTermType(termType);
        } else {
            bTermInfoVO.setCheckFlag("终端类型为必填项；");
            bTermInfoVO.setColName("终端类型");
            bTermInfoVO.setColumns(1);
            return bTermInfoVO;
        }
        String brandCode = bTermInfoVO.getBrandCode();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(brandCode)) {
            /*根据厂商名称查询厂商编号*/
            brandCode = brandCode.substring(0, 8);
            bTermInfoVO.setBrandCode(brandCode);
        } else {
            bTermInfoVO.setCheckFlag("终端厂商为必填项；");
            bTermInfoVO.setColName("终端厂商");
            bTermInfoVO.setColumns(2);
            return bTermInfoVO;
        }
        String modelCode = bTermInfoVO.getModelCode();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(modelCode)) {
            modelCode = modelCode.substring(0, modelCode.indexOf("-"));
            bTermInfoVO.setModelCode(modelCode);
        } else {
            bTermInfoVO.setCheckFlag("终端型号为必填项；");
            bTermInfoVO.setColName("终端型号");
            bTermInfoVO.setColumns(3);
            return bTermInfoVO;
        }
        if (Common.isNotBlank(bTermInfoVO.getStartTime())) {
            bTermInfoVO.setStartTime(bTermInfoVO.getStartTime() + " 00:00:00.000");
        }
        if (Common.isNotBlank(bTermInfoVO.getTermTxnStarWay())) {
            String termTxnStarWay = bTermInfoVO.getTermTxnStarWay();
            termTxnStarWay = termTxnStarWay.substring(0, termTxnStarWay.indexOf("-"));
            bTermInfoVO.setTermTxnStarWay(termTxnStarWay);
        }
        if (Common.isNotBlank(bTermInfoVO.getTermDevcTp())) {
            String termDevcTp = bTermInfoVO.getTermDevcTp();
            termDevcTp = termDevcTp.substring(0, termDevcTp.indexOf("-"));
            bTermInfoVO.setTermDevcTp(termDevcTp);
        }
        String termAddrPrvnc = bTermInfoVO.getTermAddrPrvnc();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(termAddrPrvnc)) {
            /*省由中文翻译成数字*/
            List<RegionInfo> regionInfoLst = regionInfoService.getByName(termAddrPrvnc);
            if (null != regionInfoLst && regionInfoLst.size() > 0) {
                termAddrPrvnc = regionInfoLst.get(0).getRegionCode();
                bTermInfoVO.setTermAddrPrvnc(termAddrPrvnc);
            } else {
                bTermInfoVO.setCheckFlag("布放实体店-省错误；");
                bTermInfoVO.setColName("布放实体店-省");
                bTermInfoVO.setColumns(8);
                return bTermInfoVO;
            }
        } else {
            bTermInfoVO.setCheckFlag("布放实体店-省为必填项；");
            bTermInfoVO.setColName("布放实体店-省");
            bTermInfoVO.setColumns(8);
            return bTermInfoVO;
        }
        String termAddrCity = bTermInfoVO.getTermAddrCity();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(termAddrCity)) {
            /*省由中文翻译成数字*/
            List<RegionInfo> regionInfoLst = regionInfoService.getByName(termAddrCity);
            if (null != regionInfoLst && regionInfoLst.size() > 0) {
                termAddrCity = regionInfoLst.get(0).getRegionCode();
                bTermInfoVO.setTermAddrCity(termAddrCity);
            } else {
                bTermInfoVO.setCheckFlag("布放实体店-市错误；");
                bTermInfoVO.setColName("布放实体店-市");
                bTermInfoVO.setColumns(9);
                return bTermInfoVO;
            }
        } else {
            bTermInfoVO.setCheckFlag("布放实体店-市为必填项；");
            bTermInfoVO.setColName("布放实体店-市");
            bTermInfoVO.setColumns(9);
            return bTermInfoVO;
        }
        String termAddrArea = bTermInfoVO.getTermAddrArea();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(termAddrArea)) {
            termAddrArea = termAddrArea.substring(0, termAddrArea.indexOf("-"));
            bTermInfoVO.setTermAddrArea(termAddrArea);
        } else {
            bTermInfoVO.setCheckFlag("布放实体店-区为必填项；");
            bTermInfoVO.setColName("布放实体店-区");
            bTermInfoVO.setColumns(10);
            return bTermInfoVO;
        }
        String ShopAttr = bTermInfoVO.getShopAttr();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(ShopAttr)) {
            bTermInfoVO.setShopAttr(ShopAttr);
        } else {
            bTermInfoVO.setCheckFlag("布放实体店地址为必填项；");
            bTermInfoVO.setColName("布放实体店地址");
            bTermInfoVO.setColumns(11);
            return bTermInfoVO;
        }
        String Shoptime = bTermInfoVO.getShoptime();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(Shoptime)) {
            Shoptime += " 00:00:00.000";
            bTermInfoVO.setShoptime(Shoptime);
        } else {
            bTermInfoVO.setCheckFlag("布放实体店时间为必填项；");
            bTermInfoVO.setColName("布放实体店时间");
            bTermInfoVO.setColumns(12);
            return bTermInfoVO;
        }
        String SN = bTermInfoVO.getSn();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(SN)) {
            //校验s/n是否已入库
//            List<Map> snList = selectTermSn(instId, SN);
//            if (null == snList || snList.size() < 1) {
//                uploadErrMsg += "S/N未入库，请先入库；";
//            }
            bTermInfoVO.setSn(SN);
        } else {
            bTermInfoVO.setCheckFlag("S/N为必填项；");
            bTermInfoVO.setColName("S/N");
            bTermInfoVO.setColumns(13);
            return bTermInfoVO;
        }
//        if (Common.isNotBlank(bTermInfoVO.getContactName())) {
//            bTermInfoVO.setContactName(getCellValue(row, 14));
//        }
//        if (Common.isNotBlank(getCellValue(row, 15))) {
//            termInfo.setContactPhone(getCellValue(row, 15));
//        }
        String Longitude = bTermInfoVO.getLongitude();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(Longitude)) {
            bTermInfoVO.setLongitude(Longitude);
        } else {
            bTermInfoVO.setCheckFlag("经度为必填项；");
            bTermInfoVO.setColName("经度");
            bTermInfoVO.setColumns(16);
            return bTermInfoVO;
        }
        String Latitude = bTermInfoVO.getLatitude();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(Latitude)) {
            bTermInfoVO.setLatitude(Latitude);
        } else {
            bTermInfoVO.setCheckFlag("纬度为必填项；");
            bTermInfoVO.setColName("纬度");
            bTermInfoVO.setColumns(17);
            return bTermInfoVO;
        }
        String EffectiveRadius = bTermInfoVO.getEffectiveRadius();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(EffectiveRadius)) {
            bTermInfoVO.setEffectiveRadius(EffectiveRadius);
        } else {
            bTermInfoVO.setCheckFlag("有效半径（千米）为必填项；");
            bTermInfoVO.setColName("有效半径（千米）");
            bTermInfoVO.setColumns(18);
            return bTermInfoVO;
        }

//        if (!"".equals(uploadErrMsg)) {
//            uploadErrMsg = "第" + n + "行：" + uploadErrMsg + "</br>";
//            result.put("uploadErrMsg", uploadErrMsg);
//        }
//        termInfo.setTermNo(generateCode(user.getInstId()));
//        termInfo.setId(String.valueOf(System.currentTimeMillis()));
        //    termInfo.setInstId(org.getInstId());
//        termInfo.setOprId(user.getAuthUid());
        //    termInfo.setCrtOrgId(org.getOrgCode());
        bTermInfoVO.setCreateTime(lastUpddt);
//        termInfo.setLastOprId(user.getAuthUid());
        bTermInfoVO.setLastUpdateTime(lastUpddt);

        bTermInfoVO.setCupsReportStatus("00");
        bTermInfoVO.setTermStatus(BusinessContainer.data_sta_00);
        bTermInfoVO.setAuditStatus(BusinessContainer.status_01);

        return bTermInfoVO;
    }

    @Override
    public List<BTermInfo> selectTermNo(String status, String instId) {
        LambdaQueryWrapper<BTermInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BTermInfo::getId, instId).eq(BTermInfo::getTermStatus, status);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public BTermInfo selectByTermNo(String termNo) {
        LambdaQueryWrapper<BTermInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BTermInfo::getTermNo, termNo);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public BTermInfo getTermInfosByTmNo(String instId, String terminalNo) {
        LambdaQueryWrapper<BTermInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BTermInfo::getInstId, instId).eq(BTermInfo::getTermNo, terminalNo);
        return baseMapper.selectOne(wrapper);
    }

    //查询所有
    @Override
    public IPage<BTermInfo> termNoList() {
        LambdaQueryWrapper<BTermInfo> queryWrapper = new LambdaQueryWrapper<>();
        Page<BTermInfo> page = new Page<>();
        Page<BTermInfo> cfgPage = this.page(page, queryWrapper);
        return cfgPage;
    }

    /**
     * 法人行(3位) + 6位日期(yyMMdd) + 7位序列号，每家行的序列号分开
     *
     * @param instId 格式化之前的编码
     * @return
     */
    public String generatePswdOstVlCode(String instId) {
        // 获取渠道ID：固定20
//        String channelId = CommonUtils.getChannelId(datadictService);
        // 自动生成一级代码
        String paramCode = BusinessContainer.COMM_PARAM_CODE;
        // 生成序列号
        String seqNum = sysSeqService.updateParamValue(paramCode, "PSWDOSTVL" + instId, null);
        // 代付pos验密偏移值
        String dateStr = new SimpleDateFormat("yyyyMMdd").format(new Date());
        dateStr = dateStr.substring(2);
        String result = instId + dateStr + String.format("%07d", Integer.parseInt(seqNum));
        return result;
    }


}
